<?php
namespace app\api\controller\v1;
use app\api\controller\Common;

class Adminuser extends Common {
	var $modelDb;
	public function _initialize() {
		parent::_initialize();
		$this->modelDb = model("AdminUser");
	}
	public function add() {
		$this->postData['puserid'] = $this->uinfo['userid'];
		$id = $this->modelDb->save_data($this->postData);
		if ($id === false) {
			return $this->app_error($this->modelDb->getError());
		} else {
			$info = $this->modelDb->get_data(['item_id' => $id]);
		}
		return $this->app_success($info);
	}
	public function register() {
		$result = $this->validate($this->postData, 'AdminUser.register');
		trace($result);
		if ($result !== true) {
			return $this->app_error($result);
		}
		$id = $this->modelDb->where('register_code', $this->postData['register_code'])->update(['password' => md5($this->postData['password']), 'username' => $this->postData['username'], 'status' => 0]);
		if ($id === false) {
			return $this->app_error($this->modelDb->getError());
		}
		// model("Statistics")->add_log(1);
		// model("Jifen")->save_datas(['userid'=>$id, 'taskid'=>4]);
		$info = $this->modelDb->where('register_code', $this->postData['register_code'])->find();
		$info['token'] = model("MemberToken")->create_token($info['userid'], $info['username']);
		return $this->app_success($info);
	}
	public function login() {
		$phone = $this->postData['phone'];
		$password = md5($this->postData['password']);
		$result = $this->validate($this->postData, 'AdminUser.login_phone');
		if (true !== $result) {
			return $this->app_error($result);
		}
		$id = $this->modelDb->login_phone($phone, $password);
		if ($id === false) {
			return $this->app_error("用户名/手机号/邮箱或密码输入错误！");
		}
		model("Jifen")->save_datas($id, 1);
		$mdata = $this->modelDb->get_data(['item_id' => $id]);
		if (in_array($mdata['roleid'], [1, 4, 6])) {
			return $this->app_error('账号或密码错误');
		}
		$mdata['token'] = model("MemberToken")->create_token($id, $mdata['username']);
		// $mdata['guanzhu'] = model('Favorites')->where('userid', $id)->where('typeid', 1)->count();
		// $mdata['favs'] = model('Favorites')->where('userid', $id)->where('typeid', 2)->count();
		// $mdata['issign'] = model('Jifen')->get_sign($id);
		return $this->app_success($mdata);
	}
	public function editpwd() {
		$this->postData['phone'] = isset($this->postData['phone']) ? $this->postData['phone'] : $this->uinfo['phone'];
		$this->postData['userid'] = isset($this->uinfo['userid']) ? $this->uinfo['userid'] : 0;
		$result = $this->validate($this->postData, 'AdminUser.change_pwd');
		if ($result !== true) {
			return $this->app_error($result);
		}
		$id = $this->modelDb->forget_pwd($this->postData);
		if ($id) {
			return $this->app_success([], '修改成功');
		} else {
			return $this->app_error('修改失败');
		}
	}
	public function memberinfo() {
		$userid = isset($this->postData['userid']) && $this->postData['userid'] ? $this->postData['userid'] : $this->uinfo['userid'];
		$data = $this->modelDb->get_data(['item_id' => $userid]);
		// $data['guanzhu'] = model('Favorites')->where('userid', $userid)->where('typeid', 1)->count();
		// $data['favs'] = model('Favorites')->where('userid', $userid)->where('typeid', 2)->count();
		// $data['issign'] = model('Jifen')->get_sign($userid);
		$data['vip'] = 1;
		return $this->app_success($data);
	}
	public function get_reginfo() {
		$register_code = $this->postData['register_code'];
		$data = $this->modelDb->where('register_code', $register_code)->field('userid,phone,realname,roleid,status,puserid,roleid,gender,weixin,idcard,register_code,identity')->find();
		$data['dealer'] = $data['dealer'];
		$data['prealname'] = $data['prealname'];
		$data['identity_name'] = $data['identity_name'];
		if ($data['status'] != 99) {
			return $this->app_error('该账号已经完成注册');
		}
		return $this->app_success($data);
	}
	public function update_info() {
		$id = $this->modelDb->update_data($this->postData);
		if ($id === false) {
			return $this->app_error($this->modelDb->getError());
		}
		return $this->app_success();
	}
	public function update() {
		if (!$this->postData['value']) {
			return $this->app_error('传值不能为空');
		}
		// if(isset($this->postData['field'])){
		// 	$this->postData[$this->postData['field']] = $this->postData['value'];
		// }
		// $this->postData['userid'] = $this->uinfo['userid'];
		if ($this->postData['field'] == 'phone') {
			$count = $this->modelDb->where('phone', $this->postData['value'])->count();
			if ($count) {
				return $this->app_error('该手机号已存在');
			}
		}
		$userid = $this->modelDb->where('userid', $this->uinfo['userid'])->setField($this->postData['field'], $this->postData['value']);
		if ($userid) {
			return $this->app_success();
		} else {
			return $this->app_error();
		}
	}
	public function add_address() {
		$this->postData['userid'] = $this->uinfo['userid'];
		$id = model('MemberAddress')->save_data($this->postData);
		if ($id === false) {
			return $this->app_error(model('MemberAddress')->getError());
		}
		return $this->app_success();
	}
	public function update_address() {
		$id = model('MemberAddress')->update_data($this->postData);
		if ($id === false) {
			return $this->app_error(model('MemberAddress')->getError());
		}
		return $this->app_success();
	}
	public function del_address() {
		$id = model('MemberAddress')->where('id', $this->postData['id'])->delete();
		return $this->app_success();
	}
	public function add_poster() {
		$this->postData['userid'] = $this->uinfo['userid'];
		$this->postData['title'] = $this->postData['write_title'] ? $this->postData['write_title'] : $this->postData['select_title'];
		$this->postData['typeid'] = 1;
		$ids = model('Poster')->save_data($this->postData);
		if ($ids === false) {
			return $this->app_error(model('Poster')->getError());
		}
		return $this->app_success();
	}
	public function update_poster() {
		$this->postData['title'] = $this->postData['write_title'] ? $this->postData['write_title'] : $this->postData['select_title'];
		$this->postData['typeid'] = 1;
		$ids = model('Poster')->update_data($this->postData);
		if ($ids === false) {
			return $this->app_error(model('Poster')->getError());
		}
		return $this->app_success();
	}
	//保存分享截图
	public function sharethumb_add() {
		$this->postData['userid'] = $this->uinfo['userid'];
		$ids = model('ShareThumb')->save_data($this->postData);
		if ($ids === false) {
			return $this->app_error(model('Poster')->getError());
		}
		return $this->app_success();
	}
	//检测密码是否正确
	public function check_pwd() {
		$password = db('AdminUser')->where('userid', $this->uinfo['userid'])->value('password');
		if ($password != md5($this->postData['password'])) {
			return $this->app_error('密码输入错误，请重新输入');
		}
		$price = model('Convert')->where('id', $this->postData['goods_id'])->value('price');
		if ($price > $this->uinfo['jifen']) {
			return $this->app_error('积分不足，无法兑换');
		}
		return $this->app_success();
	}
}